package post;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

import yxnote.Category;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Post {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    
    @Persistent
    private String title;
    
    @Persistent
    private Key category;
    
    @Persistent
    private Text content;
    
    @Persistent
    private Date addDate;
    
    public Post(String title, String category, String content) {
        this.setTitle(title)
            .setCategory(category)
            .setContent(content)
            .setAddDate();
    }
    
    public Long getId() {
        return this.id;
    }
    
    public String getTitle() {
        return this.title;
    }
    
    public Key getCategory() {
        return this.category;
    }
    
    public Text getContent() {
        return this.content;
    }
    
    public Date getAddDate() {
        return this.addDate;
    }
    
    public Post setTitle(String title) {
        this.title = title;
        return this;
    }
    
    public Post setCategory(String category) {
        Key k = KeyFactory.createKey(Category.class.getSimpleName(), Long.valueOf(category));
        this.category = k;
        return this;
    }
    
    public Post setContent(String content) {
        this.content = new Text(content);
        return this;
    }
    
    public Post setAddDate() {
        this.addDate = new Date();
        return this;
    }
}
